/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.hsos.ai.swa.simplebookstore.control;

import de.hsos.ai.swa.simplebookstore.entity.Book;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author rainer
 */
@Stateless
public class BookRepository {
    @PersistenceContext(name="SimpleBookStorePU")
    EntityManager em;

    public BookRepository() {
    }
    
    public  Book addBook(Book book) {
        em.persist(book);
        return book;
    }
    
    public Book findBook(String isbn) {
        Book book = null;
        TypedQuery query = em.createQuery("select b from Book b where b.isbn=:isbn", Book.class);
        if(isbn!=null)
            book = (Book) query.setParameter("isbn", isbn).getSingleResult();
        return book;
    }
}
